package com.gitee.wsl.android.ui.theme.inject

import android.R
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.app.ActionBar
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.SwitchCompat
import androidx.core.app.NotificationCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.widget.TextViewCompat
import com.gitee.wsl.common.ui.theme.ColorPaletteScheme
import com.gitee.wsl.common.ui.theme.Theme
import com.gitee.wsl.common.ui.theme.TypographyScheme

object AndroidXViewThemeInject {
    fun <P: ColorPaletteScheme,T: TypographyScheme,THEME: Theme<P,T>> injectTheme(theme:THEME, view: View):Boolean{
        when(view){

            is SwitchCompat -> view.theme(theme)
            is AppCompatTextView -> view.theme(theme)
            is SearchView -> view.theme(theme)
            else-> return false
        }
        return true
    }
}


fun <P: ColorPaletteScheme,T: TypographyScheme,THEME: Theme<P, T>> SwitchCompat.theme(theme:THEME) {
    useTheme(this, theme.colorScheme.toAndroid()) { scheme ->
        val trackColor1 = Color.argb(
            SWITCH_COMPAT_TRACK_ALPHA,
            Color.red(scheme.primary),
            Color.green(scheme.primary),
            Color.blue(scheme.primary)
        )
        val thumbUncheckedColor = 0xececec
        val trackUncheckedColor = 0xb2b2b2

        val thumbColor = ColorStateList(
            arrayOf(intArrayOf(R.attr.state_checked), intArrayOf()),
            intArrayOf(scheme.primary, thumbUncheckedColor)
        )
        val trackColor = ColorStateList(
            arrayOf(intArrayOf(R.attr.state_checked), intArrayOf()),
            intArrayOf(trackColor1, trackUncheckedColor)
        )
        DrawableCompat.setTintList(thumbDrawable,thumbColor)
        DrawableCompat.setTintList(trackDrawable, trackColor)
    }
}

fun <P: ColorPaletteScheme,T: TypographyScheme,THEME: Theme<P,T>> AppCompatTextView.theme(theme:THEME) {
    useTheme(this, theme.colorScheme.toAndroid()) { scheme ->
        setTextColor(scheme.primary)
        TextViewCompat.setCompoundDrawableTintList(this, ColorStateList.valueOf(scheme.primary))
    }
}

fun <P: ColorPaletteScheme,T: TypographyScheme,THEME: Theme<P,T>> ActionBar.theme(theme:THEME, backArrow: Drawable) {
    useTheme(this, theme.colorScheme.toAndroid()) { scheme ->
        setBackgroundDrawable(ColorDrawable(scheme.surface))
        val indicator = colorDrawable(backArrow, scheme.onSurface)
        setHomeAsUpIndicator(indicator)
        subtitle = getColoredSpan(subtitle.toString(), scheme.onSurfaceVariant)
    }
}

fun <P: ColorPaletteScheme,T: TypographyScheme,THEME: Theme<P,T>> SearchView.theme(theme:THEME) {
    useTheme(this, theme.colorScheme.toAndroid()) { scheme ->
        val editText =findViewById<SearchView.SearchAutoComplete>(androidx.appcompat.R.id.search_src_text)
        val searchPlate = findViewById<LinearLayout>(androidx.appcompat.R.id.search_plate)
        val closeButton = findViewById<ImageView>(androidx.appcompat.R.id.search_close_btn)
        val searchButton = findViewById<ImageView>(androidx.appcompat.R.id.search_button)
        editText.setHintTextColor(scheme.onSurfaceVariant)
        editText.highlightColor = scheme.inverseOnSurface
        editText.setTextColor(scheme.onSurface)
        closeButton.setColorFilter(scheme.onSurface)
        searchButton.setColorFilter(scheme.onSurface)
        searchPlate.setBackgroundColor(scheme.surface)
    }
}

fun <P: ColorPaletteScheme,T: TypographyScheme,THEME: Theme<P,T>> NotificationCompat.Builder.theme(theme:THEME) {
    useTheme(this, theme.colorScheme.toAndroid()) { scheme ->
        color = scheme.primary
    }
}




